package cn.edu.scau.cmi.prewarning.domain;


import java.io.Serializable;

import java.lang.StringBuilder;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import java.util.Set;
import com.alibaba.fastjson.annotation.JSONField;


public class Address implements Serializable, Comparable<Address> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "discoveryaddress", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Echartevent> eventsForDiscoveryaddress;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "sourceaddress", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Echartevent> eventsForSourceaddress;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "sourceaddress", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Eventsummary> trendsForSourceaddress;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "discoveryaddress", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Eventsummary> trendsForDiscoveryaddress;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setEventsForDiscoveryaddress(Set<Echartevent> eventsForDiscoveryaddress) {
		this.eventsForDiscoveryaddress = eventsForDiscoveryaddress;
	}

	public Set<Echartevent> getEventsForDiscoveryaddress() {
		if (eventsForDiscoveryaddress == null) {
			eventsForDiscoveryaddress = new java.util.LinkedHashSet<Echartevent>();
		}
		return eventsForDiscoveryaddress;
	}

	public void setEventsForSourceaddress(Set<Echartevent> eventsForSourceaddress) {
		this.eventsForSourceaddress = eventsForSourceaddress;
	}

	public Set<Echartevent> getEventsForSourceaddress() {
		if (eventsForSourceaddress == null) {
			eventsForSourceaddress = new java.util.LinkedHashSet<Echartevent>();
		}
		return eventsForSourceaddress;
	}

	public void setTrendsForSourceaddress(Set<Eventsummary> trendsForSourceaddress) {
		this.trendsForSourceaddress = trendsForSourceaddress;
	}

	public Set<Eventsummary> getTrendsForSourceaddress() {
		if (trendsForSourceaddress == null) {
			trendsForSourceaddress = new java.util.LinkedHashSet<Eventsummary>();
		}
		return trendsForSourceaddress;
	}

	public void setTrendsForDiscoveryaddress(Set<Eventsummary> trendsForDiscoveryaddress) {
		this.trendsForDiscoveryaddress = trendsForDiscoveryaddress;
	}

	public Set<Eventsummary> getTrendsForDiscoveryaddress() {
		if (trendsForDiscoveryaddress == null) {
			trendsForDiscoveryaddress = new java.util.LinkedHashSet<Eventsummary>();
		}
		return trendsForDiscoveryaddress;
	}

	public void copy(Address that) {

		setId(that.getId());

		setName(that.getName());
		setDescription(that.getDescription());

		setEventsForDiscoveryaddress(new java.util.LinkedHashSet<Echartevent>(
				that.getEventsForDiscoveryaddress()));

		setEventsForSourceaddress(new java.util.LinkedHashSet<Echartevent>(
				that.getEventsForSourceaddress()));

		setTrendsForSourceaddress(new java.util.LinkedHashSet<Eventsummary>(
				that.getTrendsForSourceaddress()));

		setTrendsForDiscoveryaddress(new java.util.LinkedHashSet<Eventsummary>(
				that.getTrendsForDiscoveryaddress()));

	}

	public Address() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Address))
			return false;
		Address comparedAddress = (Address) obj;
		if (!CheckPropertyEqual(this.name, comparedAddress.name)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Address o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
